package com.zhaoxi.project.sys.service;

import com.zhaoxi.common.utils.DateUtils;
import com.zhaoxi.framework.web.PageData;
import com.zhaoxi.framework.web.BaseService;
import com.zhaoxi.project.sys.mapper.DictMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

//样例
@Service
public class DictService extends BaseService {
  @Autowired
  private DictMapper dictMapper;
  //查询数据集合
  public List<PageData> getList(PageData pd) {
    return dictMapper.getList(pd);
  }


  //查询数据对象 通过id
  public PageData getById(PageData pd) {
    PageData pd1 = new PageData();
    if (pd.getLong("id") == null){
      return null;
    }
    pd1.put("id",pd.getLong("id"));
    List<PageData> list = dictMapper.getList(pd);
    if (list.size() > 0){
      return list.get(0);
    }
    return null;
  }


  //查询数据集合
  public PageData dictGetListByCode(PageData pd) {

    List<PageData> list = dictMapper.getList(pd);

    PageData pd1 = new PageData();
    if (list.size() > 0){
      pd1 = list.get(0);
    }else{
      return this.error("未查询到该编码的数据字典！！！");
    }

    PageData pd2 = new PageData();
    list.clear();
    if (pd1.getLong("parentId") == 0){
      //用上级的id去查询所以父类
      pd2.put("parentId",pd1.getLong("id"));
    } else if (pd1.getLong("parentId") == 1){
      //用上级的id去模糊查询所以祖籍
      pd2.put("ancestors",pd1.getLong("id"));
    } else {
      return this.error("该编码并不是数据字典的编码！！！");
    }
    pd2.put("tenantId",pd1.getLong("tenantId"));
    list = dictMapper.getList(pd2);

    return this.responses(list);

  }

  //查询数据行数
  public PageData getCount(PageData pd) {

    return this.success( dictMapper.getCount(pd));

  }

  //新增表格数据
  public PageData addTabs(PageData pd) {

//    临时用
    PageData pd1 = new PageData();
    pd1.put("code",pd.getString("code"));
    pd1.put("parentId",pd.getLong("parentId"));
    pd1.put("tenantId",pd.getLong("tenantId"));
    if (this.dictMapper.getCount(pd1) > 0){
      return this.error("数据字典编码不能重复！！！");
    }
    return this.responses(dictMapper.add(pd));

  }
  //修改
  public PageData editTabs(PageData pd) {

//  判断一下修改的编码是否和本组其他的重复
    PageData pd1 = new PageData();
    pd1.put("code",pd.getString("code"));
    pd1.put("parentId",pd.getLong("parentId"));
    pd1.put("tenantId",pd.getLong("tenantId"));
    List<PageData> list = this.dictMapper.getList(pd1);
    int i = 0;
    while ( list.size() > i){
      pd1.clear();
      pd1 = list.get(i);
      if (! pd1.getString("id").equals(pd.getString("id"))){
        return this.error("数据字典编码不能重复！！！");
      }
      i++;
    }
    return this.responses(dictMapper.edit(pd));

  }

  //新增
  public PageData addTree(PageData pd) {

//    临时用
    PageData pd1 = new PageData();
    if (pd.getLong("parentId") == null){
      pd.put("parentId",0);
    }
    pd1.put("id",pd.getLong("parentId"));
    List<PageData> pdList = dictMapper.getList(pd1);
    pd1.clear();
    if (pdList.size()>0){
      pd1 = pdList.get(0);
    }else{
      pd1.put("ancestors","0,");
    }
    String ancestors = pd1.getString("ancestors");
    pd.put("ancestors",ancestors+pd.getString("parentId")+",");
    //插入数据
    pd.put("createTime",DateUtils.getNowDate());

    return this.responses(dictMapper.add(pd));

  }
  //修改
  public PageData editTree(PageData pd) {


    return this.responses(dictMapper.edit(pd));

  }
  //删除
  public PageData del(Long id) {

    return this.responses(dictMapper.del(id));

  }
}
